Code
install.packages("ggplot2")
library(ggplot2)Welcome! This website contains a collection of graphs composed in using ggplot2 (and friends!).
Install ggplot2 using the code below:
install.packages("ggplot2")
library(ggplot2)Or install ggplot2 as part of the tidyverse:
install.packages("tidyverse")
library(tidyverse)To improve reproducibility, the majority of the graphs are built using the palmerpenguins::penguins data.
…so…many…PENGUINS!
install.packages("palmerpenguins")
library(palmerpenguins)Some of the graphs use datasets from the fivethirtyeight package.
install.packages("fivethirtyeight")
library(fivethirtyeight)A few of the graphs are built using the ggplot2movies::movies data.
install.packages("ggplot2movies")
library(ggplot2movies)The gallery follows a Rule of Least Power Principle, in the sense that “a language with a straightforward syntax may be easier to analyze than an otherwise equivalent one with more complex structure.”
In other words, assuming the reader has some understanding of R and the tidyverse, the code for each graph is meant to be read and understood without having to run it.
Each graph has the same four sections and structure: Packages, Data, Code, and Graph
PACKAGES:
Install packages.
library(ggplot2)DATA:
Description of data
df <- tibble::tibble(X = sample(x = 1:100, 10, FALSE),
Y = rlnorm(10, 1, 3))CODE:
Code for creating graph
# labels
labs_graph <- ggplot2::labs(title = "Title",
subtitle = "subtitle",
x = "X", y = "Y")
# layers
ggp2_graph <- ggplot2::ggplot(data = df,
mapping = aes(x = X, y = Y)) +
ggplot2::geom_blank()
# graph
ggp2_graph +
labs_graphGRAPH:
All objects have the following naming conventions:
Graph labels have the labs_ prefix
Graph layers have a ggp2_ prefix
Code Style:
I’ve attempted to balance brevity and clarity, but with the assumption that its best to err on the latter. I’ve also followed the general principle that if a graph can be easily built using one of ggplot2 ’s geom_* functions, that method is shown first.
If the graph can’t be built with ggplot2’s geoms and requires additional packages/geoms/functions, instructions for installing the development version for these packages has been provided.
Extensions:
Below are the graphs requiring additional packages/extensions:
Waffle charts
# Waffle Charts
devtools::install_github("liamgilbey/ggwaffle")
library(ggwaffle)Pie Charts
# Pie Charts
devtools::install_github("kassambara/ggpubr")
library(ggpubr)Mosaic Plots
# Mosaic Charts
devtools::install_github("haleyjeppson/ggmosaic")
library(ggmosaic)Treemaps
# Treemaps
devtools::install_github("wilkox/treemapify")
library(treemapify)Bee-swarm Plots
# Bee-swarm Plots
devtools::install_github("eclarke/ggbeeswarm")
library(ggbeeswarm)Ridgeline Plots
# Ridgeline plots
devtools::install_github("wilkelab/ggridges")
library(ggridges)Rain-cloud plots
raincloudplots and ggdist packages
# Rain-cloud plots
remotes::install_github('jorvlan/raincloudplots')
remotes::install_github('mjskay/ggdist')
library(raincloudplots)
library(ggdist)Alluvial charts
# Alluvial charts
devtools::install_github("corybrunson/ggalluvial")
library(ggalluvial)Bump charts
# Bump charts
devtools::install_github("davidsjoberg/ggbump")
library(ggbump)Parallel Sets
# Bump charts
devtools::install_github("thomasp85/ggforce")
library(ggforce)Graphs have been categorized into the following types:
Some graphs would justifiably belong in more than one category, and wherever this is the cases, I’ve tried to include other uses in the notes.